package com.example.tnot.Util;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.tnot.R;

public class ToastUtil {
    private static Toast currentToast;

    public static void showCustomToast(Context context, String message) {
        showCustomToast(context, message, Toast.LENGTH_SHORT);
    }

    public static void showCustomToast(Context context, String message, int duration) {
        // 取消当前正在显示的Toast
        if (currentToast != null) {
            currentToast.cancel();
        }

        try {
            // 创建新的Toast
            LayoutInflater inflater = LayoutInflater.from(context);
            View layout = inflater.inflate(R.layout.mytoast_layout, null);

            TextView tv_mytoast = layout.findViewById(R.id.tv_mytoast);
            tv_mytoast.setText(message);

            currentToast = new Toast(context);
            currentToast.setView(layout);
            currentToast.setGravity(Gravity.CENTER, 0, 450);
            currentToast.setDuration(duration);
            currentToast.show();
        } catch (Exception e) {
            // 如果自定义Toast出现问题，使用系统默认Toast
            Toast.makeText(context, message, duration).show();
        }
    }
}
